<?php 

#	Fastfoo - CMS developer under CakePHP / http://fast-foo.com
#	Copyright (C) 2010  Antonio Irlandés García

#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.

#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.

#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.


/**
 * Renderer helper
 *
 * Encargado de pintar en la vista todo lo que se le diga
 */
class RendererHelper extends AppHelper {

	var $content_render;//Contenido a renderizar



	/**
	 * Encargado de dibujar un menu en arbol
	 *
	 * $array_tree array con los hijos del arbol a pintar
	 * $options array con las opciones para el padre del arbol
	 */
	function menuTree($array_tree, $options){		

		$this->content_render	.= '<ul ';
		//Repasa todas las opciones pasadas
		if(is_array($options) && !empty($options)){
			foreach ($options as $key => $value){
				if(!empty($value)){
					$this->content_render	.= ' '.$key.'="'.$value.'" ';
					//vamos borrando los valores para que no vuelva a pasar por ellos solo la primera vez
					unset($options[$key]);
				}
			}
		}
		$this->content_render	.= '>';

		//Total de hijos de la rama actual
		$total = count($array_tree);
		
		
		$i = 0;
		foreach ($array_tree as $branch){

			//nuevo Hijo
			$this->content_render	.= '<li';	
			//El último de los hijos	
			if($total == $i + 1)
				$this->content_render	.= ' class="last" ';
			$this->content_render	.= '>';
		
			$this->content_render	.= '<a href="'.$branch['Menu']['url'].'"';

			//Se abre en ventana nueva
			if($branch['Menu']['blank'] == 1)
				$this->content_render	.= ' target="_blank" ';

			//Es padre
			if(count($branch['Children']) > 0)
				$this->content_render	.= ' class="sub" ';
			$this->content_render	.= '>';

			$this->content_render	.= $branch['Menu']['name']."</a>";
			
			if(count($branch['Children']) > 0){
				$this->menuTree($branch['Children'], $options = null);
				$this->content_render	.= "</li>";
			}else{
				$this->content_render	.= "</li>";
			}
			$i++;
		}
		
		$this->content_render	.= "</ul>";	

		echo $this->content_render;

		$this->content_render = null;

	}
}
?>
